﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace Alvin.Window.Utils
{
    using Microsoft.Win32;

    public static class Util
    {
        public static void SetWindowCenterScream(System.Windows.Window window)
        {
            if(window == null) return;
            var screeHeight = SystemParameters.WorkArea.Height;
            var screeWidth = SystemParameters.WorkArea.Width;
            window.Top = (screeHeight - window.Height)/2;
            window.Left = (screeWidth - window.Left)/2;
        }
    }

    public class WindowPositionHelper
    {
        public static string RegPath = @"Software\MyApp\WindowBounds\";

        public static void SaveSize(System.Windows.Window win)
        {
            // Create or retrieve a reference to a key where the settings
            // will be stored.
            RegistryKey key;
            key = Registry.CurrentUser.CreateSubKey(RegPath + win.Name);
            key.SetValue("Bounds", win.RestoreBounds.ToString());
        }

        public static void SetSize(System.Windows.Window win)
        {
            RegistryKey key;
            key = Registry.CurrentUser.OpenSubKey(RegPath + win.Name);

            if (key == null) return;
            var bounds = Rect.Parse(key.GetValue("Bounds").ToString());
            win.Top = bounds.Top;
            win.Left = bounds.Left;

            // Restore the size only for a manually sized window.
            if (win.SizeToContent != SizeToContent.Manual) return;
            win.Width = bounds.Width;
            win.Height = bounds.Height;
        }
    }

}
